/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.actions.componentes;

import com.mapping.Componentes;
import com.opensymphony.xwork2.ActionSupport;
import static com.opensymphony.xwork2.Action.SUCCESS;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
/**
 *
 * @author Andres
 */
@Namespace(value = "/")
@Action(value = "buscarComponentes", results = {@Result(name = "success", location = "/GestionComponentes/gestionComponentes.jsp")})
public class BuscarComponentesAction extends ActionSupport{
    private List<Componentes> listaComponentes;
    private String buscar;
    @Override
    public String execute(){
        String consulta = "select c.ID, c.IDTIPO, c.IDDISPORIGEN, c.IDDISPACTUAL, c.CARACTERISTICAS, c.STATUS "
                            + "from componentes as c "
                            + "inner join tipo_disp td on (c.idTipo=td.idTipo) "
                            + "where match (c.idDispOrigen,c.idDispActual,c.caracteristicas,c.status) against (?1 IN BOOLEAN MODE) "
                            + "or match (td.tipo) against (?1 IN BOOLEAN MODE) "; 
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("inventarios");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        List<Componentes> componentes;
        if(buscar== null || buscar.equals("")){
            listaComponentes = (List<Componentes>) em.createNamedQuery("Componentes.findAll").getResultList();
        }else{
            Query query =  em.createNativeQuery(consulta,Componentes.class);
            query.setParameter(1,buscar);
            listaComponentes = (List<Componentes>) query.getResultList();
        }

        tx.commit();
        em.close();
        return SUCCESS;
    }

    public void setBuscar(String buscar) {
        this.buscar = buscar;
    }

    public List<Componentes> getListaComponentes() {
        return listaComponentes;
    }
    
}
